package com.edu.study.base.wordLearn.service.impl;

import com.edu.study.base.wordLearn.domain.EduWordLearn;
import com.edu.study.base.wordLearn.mapper.EduWordLearnMapper;
import com.edu.study.base.wordLearn.service.IEduWordLearnService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 生词库，需要学习的词汇 正在学习的词汇 1、在“目标生词”库中顺序默认100个生词Service业务层处理
 * 
 * @author admin
 * @date 2021-08-04
 */
@Service
public class EduWordLearnServiceImpl implements IEduWordLearnService 
{
    @Autowired
    private EduWordLearnMapper eduWordLearnMapper;
 
    @Override
    public EduWordLearn selectEduWordLearnById(String id)
    {
        return eduWordLearnMapper.selectEduWordLearnById(id);
    }

 
    @Override
    public List<EduWordLearn> selectEduWordLearnList(EduWordLearn eduWordLearn)
    {
        return eduWordLearnMapper.selectEduWordLearnList(eduWordLearn);
    }
 
    @Override
    public int insertEduWordLearn(EduWordLearn eduWordLearn)
    {
        return eduWordLearnMapper.insertEduWordLearn(eduWordLearn);
    }

	@Override
	public int insertEduWordLearnList(List<EduWordLearn> eduWordLearnList) {
		return eduWordLearnMapper.insertEduWordLearnList(eduWordLearnList);
	}


	@Override
    public int updateEduWordLearn(EduWordLearn eduWordLearn)
    {
        return eduWordLearnMapper.updateEduWordLearn(eduWordLearn);
    }

	@Override
	public int updateWordStatusByIds(EduWordLearn eduWordLearn) {
		return eduWordLearnMapper.updateWordStatusByIds(eduWordLearn);
	}


	@Override
    public int deleteEduWordLearnByIds(String[] ids)
    {
        return eduWordLearnMapper.deleteEduWordLearnByIds(ids);
    }
 
    @Override
    public int deleteEduWordLearnById(String id)
    {
        return eduWordLearnMapper.deleteEduWordLearnById(id);
    }
    
    @Override
	public Map<String, Object> getDayNum(String memberId, String day) { 
		final String pattern = "yyyy-MM-dd"; 
		final SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		Date todaydate = new Date(); 
		int past = Integer.valueOf(day);
		String result = this.getPastDate(past);
		String startTime = result;
		String endTime = formatter.format(todaydate);
		
		// 求不同时间段内词汇量
		List<EduWordLearn> datalist = eduWordLearnMapper.getDayNum(memberId,startTime,endTime); 
		// 标题文本
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("code", "200");

		// TODO时间段
		List<String> xAxisData = new ArrayList<>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if(CollectionUtils.isEmpty(datalist)) {
			data.put("data", list);
			data.put("xAxis", xAxisData); // 时间段
			return data;
		}
		List<Integer> values = null;
		String date = null;
		Map<String, Object> orgMap = null;
		int count = 0;
		for(EduWordLearn vo : datalist) {
			// 时间转换
			Date createDate =  vo.getCreateDate();
			String createDateStr = formatter.format(createDate);
			if(date == null || !createDateStr.equals(date)) {
				date = createDateStr;
				orgMap = new HashMap<>();
				orgMap.put("legend", date);
				values = new ArrayList<>();
				values.add(vo.getCount());
				orgMap.put("count", values); //词汇量
				list.add(orgMap);
			} else {
				values.add(vo.getCount());
			}
			if(!xAxisData.contains(createDateStr)) {
				xAxisData.add(createDateStr);
			}
		}
		
		data.put("data", list);
		List<String> sublist = new ArrayList<String>();
		for (int i = 0; i < xAxisData.size(); i ++) {
			String str = xAxisData.get(i);
			String newStr = str.replaceAll("-", ".").substring(str.length()-5, str.length());
	 		if(newStr.startsWith("0")) {
	 			newStr = str.replaceAll("-", ".").substring(str.length()-4, str.length());
			} 
	 		sublist.add(newStr); 
		} 
		data.put("xAxis", sublist); // 时间段
		return data; 
	
	}
	
	
	
	/**
	 * 一.获取过去第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作)
	     * @Title: getPastDate
	     * @Description: TODO(这里用一句话描述这个方法的作用)
	     * @param @param past
	     * @param @return 参数
	     * @return String 返回类型
	     * @throws
	 */
	private  String getPastDate(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String result = format.format(today);
		return result;

	}
   
}
